/**
 * 高级源代码
 */
(function(E){
//插件自己的iframe dom对象
var Consts = E.consts, dom = null, Utils = E.mod("Utils");

E.plugin("Source2", {
	icon: {
		width: 50,
		text: '源代码',
		
		'default': {
			'Source2': {XY:"none" }
		}
	},
	config:{
		width: 800,
		height:500,
		position: 'center',
		overlay: true
	},
	
	// 在插件初始化时执行
	init: function( self ){
		self.cache['html'] = '<div class="jqe-plugin-Source2"></div>';
	},
	
	one: function(panel, self){
		var container = panel.find(".jqe-plugin-Source2"),
			option = E.get("option"),
			o = {
				id: 'Source2',
				contentEditable: true,
				iframeStyle: {width:'99%', height:'500px', margin:'0 5px'},
				contentCssName: option.contentCssName
			};
			
		var obj = E.iframe(o, container, '');
		dom = obj.dom;
		obj.win.focus();
		
		if( Consts.IE ){
			$(dom.body).keydown(function(e){
				if (e.keyCode == 13) {
					try{
						var range = dom.selection.createRange();
						if( range.select ){range.select();}
						range.pasteHTML('<br>');
					}catch(e){}
					
					return false;
				}
			});
		}
		/*
		var Sel = new E.Selection(dom, obj.win),
			timeid = 0;
		
		$(dom).bind('keyup', function( e ){
			clearTimeout(timeid);
			timeid = setTimeout(function(){
				//dom.execCommand('inserthtml', false, '_A');
				Sel.set('_A');
				var target = Sel.getElementByCursor(),
					pre = $(target).closest("pre"),
					code = pre.text();
					
				code = Utils.escape( code );
				code = formatCodeColor(code);
				//code = code.replace('[$]', '<a class="jqe-cursor"> </a>');
				pre.html( code );
				setTimeout(function(){
					//var a = $("a.jqe-cursor", dom);
					//Sel.cursorToNext(a[0]);
					//a.remove();
					//Sel.selectNode(a[0], true);
					
					Sel.selectedText('_A');
					//dom.execCommand('inserthtml', false, '<a class="jqe-cursor"> </a>');
					//$("a.jqe-cursor", dom).remove();
					//var range = Sel.getRange();
					//range.deleteContents();
					
					//range.collapse(true);
					
					Sel.clear();
				}, 0);
			}, 2000);
		});
		*/
	},
	
	callback: function(panel, self){
		var html = E.getHTML(false);
		html = html || '<p>\n\n</p>';
		html = html.replace(/\n+/g, '');
		
		var tmpBody = $('<div></div>').append( html ),
			itemHtml='',
			child = null;
			
		tmpBody.children().each(function(i, el){
			child = $(el);
			itemHtml = child.html();
			if(itemHtml.length > 50){
				child.html('\n'+itemHtml+'\n');
			}
			child.after('\n');
		});
		
		tmpBody.find("table").find("tr,td,th").after('\n').before('\n');
		
		html = tmpBody.html();
		html = html.replace(/\n+/g, '\n');
		html = Utils.escape( html );
	//	html = html.replace(/\n/g,'<br>');
		html = html.replace(/&lt;BR&gt;/ig, '&lt;br&gt;<br>');
	//	html = html.replace(/&lt;\/p&gt;/ig, '&lt;/p&gt;<br>');
		html = html.replace(/&lt;h2&gt;/ig, '<b style="font-size:18px;">&lt;h2&gt;');
		html = html.replace(/&lt;\/h2&gt;/ig, '&lt;/h2&gt;</b>');
		
		//html = html.replace(/&lt;h3&gt;/ig, '<br><b>&lt;h3&gt;');
		html = html.replace(/&lt;\/h3&gt;/ig, '&lt;/h3&gt;</b>');
		
		html = html.replace(/&lt;.+?&gt;/ig, function($0){
			$0 = $0.replace(/COLOR:/g, 'color:');
			$0 = $0.replace(/(\w+)=/gi, '<span style="color:red">$1</span>=');
			$0 = $0.replace(/rgb\(\d+,(?: |&nbsp;)?\d+,(?: |&nbsp;)?\d+\)/ig, function($0){
				return Utils.colorToHex($0);
			});
			return '<span style="color:blue">'+$0+'</span>';
		});
		
		// IE下将标签转为小写
		if( Consts.IE ){
			html = html.replace(/&lt;BR&gt;/g, '&lt;br&gt;');
			html = html.replace(/&lt;(\w+)/ig, function($0, tagName){
				return '&lt;'+tagName.toLowerCase();
			});
			html = html.replace(/(\w+)&gt;/ig, function($0, tagName){
				return tagName.toLowerCase()+'&gt;';
			});
		}
		
		html = "<pre>"+html.split("\n").join("</pre><pre>") + '</pre>';
		
		dom.body.innerHTML = html;
	},
	
	ok: function( panel, self ){
		var html = $(dom.body).text();
		
		// 2012-01-22 潘雪鹏
		// 必须执行下面的替换，否则图片和表格会看不到，原因不明
		html = html.replace(/[\xA0]/g, ' ');
		
		E.setHTML(html);
		return 1;
	}
});

function formatCodeColor( code ){
	
	code = code.replace(/&lt;.+?&gt;/ig, function($0){
		$0 = $0.replace(/COLOR:/g, 'color:');
		$0 = $0.replace(/(\w+)=/gi, '<span style="color:red">$1</span>=');
		$0 = $0.replace(/rgb\(\d+,(?: |&nbsp;)?\d+,(?: |&nbsp;)?\d+\)/ig, function($0){
			return Utils.colorToHex($0);
		});
		return '<span style="color:blue">'+$0+'</span>';
	});
	
	return code;
}

})(jQEditor);